package com.sunther.document.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.sunther.document.entity.FileSystemMidEntity;
import com.sunther.document.entity.TermEntity;
import com.sunther.document.service.DictService;
import com.sunther.document.service.TermService;
import com.sunther.idb.common.util.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/19 9:58
 */
@RestController
@RequestMapping("/term")
public class TermController {

    @Autowired
    private TermService termService;

    @Autowired
    private DictService dictService;

    @GetMapping("/page_list")
    public WebResult pageList(@RequestParam String keyword,
                              @RequestParam(required = false) String projectId,
                              @RequestParam(required = false) Integer speciality,
                              @RequestParam("pageNumber") int pageNumber,
                              @RequestParam("pageSize") int pageSize) {
        try {
            return WebResult.success(termService.pageList(keyword,projectId,speciality,pageNumber,pageSize));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @PostMapping("/save")
    public WebResult save(@RequestBody TermEntity data,
                          HttpServletRequest request) {
        try {
            return termService.saveData(data, request);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/export")
    public void export(@RequestParam String keyword,
                       @RequestParam(required = false) String projectId,
                       @RequestParam(required = false) Integer speciality,
                       HttpServletResponse response) throws Exception {
        termService.export(keyword,projectId,speciality,response);
    }

    @GetMapping("/delete")
    public WebResult delete(@RequestParam Integer id){
        try {
            termService.removeById(id);
            return WebResult.success("操作成功");
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }

    }

    @GetMapping("/dtl")
    public WebResult dtl(@RequestParam Integer id){
        try {
            return WebResult.success("操作成功",termService.dtl(id));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }

    }

    @PostMapping("/upload")
    public WebResult upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        try {
            return termService.upload(file,request);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }
}
